package com.ncepu.clubmanagement.controller;

import com.ncepu.clubmanagement.anno.CommitteePermission;
import com.ncepu.clubmanagement.pojo.MultiMediaFontOfGet;
import com.ncepu.clubmanagement.pojo.MultiMediaFontOfGetInHead;
import com.ncepu.clubmanagement.pojo.PageBean;
import com.ncepu.clubmanagement.pojo.Result;
import com.ncepu.clubmanagement.service.MultiMediaService;
import com.ncepu.clubmanagement.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/multiMedia")
@Slf4j
public class MultiMediaController {

    @Autowired
    private MultiMediaService multiMediaService;

    @Autowired
    private JwtUtils jwtUtil;

    @Autowired
    private HttpServletRequest request;


    @GetMapping()
    public Result getMultiMediaByMultiMediaId(Integer multiMediaId){
        log.info("获取多媒体信息，多媒体id为：{}", multiMediaId);
        MultiMediaFontOfGet mediaInfo = multiMediaService.getMultiMediaFontOfGetByMultiMediaId(multiMediaId);

        return Result.success(mediaInfo);
    }

    @GetMapping("/self")
    public Result getSelfMultiMediaInfo(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5")Integer pageSize){
        // 解析jwt信息
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer personId = (Integer) claims.get("id");

        PageBean pageBean = multiMediaService.getMultiMediaFontOfGetInList(page, pageSize, personId);

        return Result.success(pageBean);
    }

    @GetMapping("/all")
    @CommitteePermission  // 管理员专用
    public Result getAllMultiMediaInfo(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5")Integer pageSize){
        PageBean pageBean = multiMediaService.getMultiMediaFontOfGetInList(page, pageSize, null);
        return Result.success(pageBean);
    }

    @DeleteMapping()
    public Result deleteMultiMedia(Integer multiMediaId){
        // 解析jwt信息
        String jwt = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(jwt);
        Integer personId = (Integer) claims.get("id");

        // 判断该多媒体信息是否属于当前用户
        if(!multiMediaService.isAuthor(personId, multiMediaId)){
            return Result.error("无权删除该多媒体信息");
        }
        multiMediaService.deleteMultiMedia(multiMediaId);

        return Result.success();
    }

    @PutMapping("/isSuggested")
    @CommitteePermission
    public Result updateMultiMediaIsSuggested(@RequestParam Integer isSuggested, @RequestParam Integer multiMediaId){
        if (isSuggested == null){
            return Result.error("参数错误");
        }

        multiMediaService.changeMultiMediaSuggestStation(isSuggested,multiMediaId);
        return Result.success();

    }

    @GetMapping("/inStyle")
    public Result getMultiMediaInStyle(@RequestParam Integer clubId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5")Integer pageSize){
        PageBean pageBean = multiMediaService.getMultiMediaFontOfGetInStyle(page, pageSize, clubId);
        return Result.success(pageBean);
    }

    @GetMapping("/inHomePage")
    public Result getMultiMediaInHomePage(@RequestParam Integer articleNum, @RequestParam  Integer videoNum){
        List<MultiMediaFontOfGetInHead> ls = multiMediaService.getMultiMediaFontOfGetInHomePage(articleNum, videoNum);
        return Result.success(ls);
    }

}
